package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.R;
import com.lc.afbx.adapters.ProviceAdapter;
import com.lc.afbx.conn.GetApiAreaIndex;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/30.
 */
public class ChoseAreaActivity extends BaseActivity implements View.OnClickListener {


    private ProviceAdapter adapter;
    private List<GetApiAreaIndex.Data> dataList;

//    private String[] ids = new String[4];
//    private String[] titles = new String[4];


    private List<Map<String, String>> areaInfoList = new ArrayList<>();

    private Map<String, String> idAndNameMap ;

    private int i = -1;

    private GetApiAreaIndex getApiAreaIndex = new GetApiAreaIndex(new AsyCallBack<GetApiAreaIndex.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiAreaIndex.Info info) throws Exception {
            dataList = info.dataList;
            proviceList.setAdapter(adapter = new ProviceAdapter(info.dataList, context));

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            close();

        }
    });
    private ListView proviceList;
    private RelativeLayout rLayoutBack;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_area_provice_jz);

        initView();

        setListener();
        getApiAreaIndex.parentid = "0";
        getApiAreaIndex.execute(this);

//        ids[i] = "0";


    }

    private void setListener() {
        rLayoutBack.setOnClickListener(this);


        proviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (++i < 4) {

                    idAndNameMap = new HashMap<String, String>();

                    idAndNameMap.put("id", dataList.get(position).id);
                    idAndNameMap.put("title", dataList.get(position).title);

                    areaInfoList.add(i, idAndNameMap);

                    title.setText(dataList.get(position).title);

                    getApiAreaIndex.parentid = dataList.get(position).id;
                    getApiAreaIndex.execute(ChoseAreaActivity.this);

                }

                //这个用来区分从哪个页面跳过来的
                String str = getIntent().getStringExtra(PrefrenceConfig.INTENT_TAG);

                //为空不改变
                if (str == null || str.isEmpty()) {
                    if (i == 3) {
                        close();
                    }
                }
//                不为空
                switch (str) {
                    case "area":
                        if (i == 2) {
                            close();
                        }
                        return;

                    case "city":
                        if (i == 1) {
                            close();
                        }
                        return;
                }


            }
        });


    }

    private void close() {

        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        Bundle bundle = new Bundle();
//        bundle.putStringArray("titles", titles);
//        bundle.putStringArray("ids", ids);

        bundle.putSerializable("", (Serializable) areaInfoList);

        ArrayList bundleList = new ArrayList();

        bundleList.add(areaInfoList);

        bundle.putParcelableArrayList("list", bundleList);

        intent.putExtras(bundle);
        //设置返回数据
        ChoseAreaActivity.this.setResult(3, intent);

        UtilLog.e("==========", "地区类里的  走没走啊");
        //关闭Activity
        this.finish();
    }

    private void initView() {
        proviceList = ((ListView) findViewById(R.id.choose_provice));

        rLayoutBack = ((RelativeLayout) findViewById(R.id.back));

        title = ((TextView) findViewById(R.id.title_area));
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:

                i = i - 1;

                if (i < -1) {
                    finish();
                    return;
                } else {

                    if (i == -1) {
                        title.setText("选择投保地区");
                        getApiAreaIndex.parentid = "0";
                        getApiAreaIndex.execute(ChoseAreaActivity.this);
                    } else {
                        
                        title.setText(areaInfoList.get(i).get("title"));

                        getApiAreaIndex.parentid = areaInfoList.get(i).get("id");
                        getApiAreaIndex.execute(ChoseAreaActivity.this);

                        areaInfoList.remove(i);
                    }


                }
                break;
        }
    }
}
